package com.mes.web.controller.lc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.lc.MaterialComeku;
import com.mes.entity.tdd.ProcurementRuku;
import com.mes.entity.tdd.ProcurementRukuMateriel;
import com.mes.service.lc.MaterialComeKuService;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * 资材入库实体类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/matcc")
public class MaterialComeKuController {

	@Autowired
	MaterialComeKuService materialComeKuService;
	
	
	/**
	 * 多条件分页查询
	 * @param stockfy
	 * @param response
	 * @param limit
	 * @param offset
	 * @throws Exception
	 */
	@RequestMapping("/selectfymck")
	public @ResponseBody void selectfymck(
			MaterialComeku materialComeku,HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset)
			throws Exception {
		
			materialComeku.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
			
			// 每页条数
			materialComeku.setPageSize(Integer.parseInt(limit));
	
			if (StringUtil.isEmpty(materialComeku.getOdd())) {
				materialComeku.setOdd(null);
			}//入库单号
			if (StringUtil.isEmpty(materialComeku.getPici())) {
				materialComeku.setPici(null);
			}//入库批次
	
			// 物料
			List<MaterialComeku> cslist = materialComeKuService.selectfymck(materialComeku);
			int a = materialComeKuService.selectfymckcont(materialComeku);
			JsonConfig config = new JsonConfig();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(cslist, config);
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", a);
			ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * PDA接口      查询所有未入库的资材收货单
	 */
	@RequestMapping("/selectProcurementall")
	@ResponseBody
	public Map<String,Object> selectProcurementall(HttpServletResponse response, HttpServletRequest request) throws Exception{
		List<ProcurementRuku> list = materialComeKuService.selectProcurementall();
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("success", "1");
		map.put("data",list);
		return map;
	}
	
	
	/**
	 * PDA 接口，拿到一个收货单id，然后给出对应的具体信息
	 * 
	 */
	@RequestMapping("/selectPrmall")
	public void selectPrmall(Long ids, HttpServletResponse response, HttpServletRequest request)
			throws Exception {

		//根据收货单id查询对应的具体信息
		List<ProcurementRukuMateriel> list = materialComeKuService.selectPrmall(ids);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", "1");
		jsonObject.put("data", list);
		ResponseUtil.write(response, jsonObject);
			
	}
	
	
	/**
	 * PDA  开始入库
	 * @param ids 收货单主键
	 * @param suprname 供应商
	 * @param number 收货单编号
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/savemck")
	public void savemck(Long ids,String suprname,String number,HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		
		//根据收货单id查询对应的具体信息
		List<ProcurementRukuMateriel> list = materialComeKuService.selectPrmall(ids);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		String nowtime = dateFormat.format(now);
		int a = 0 ;
		for(ProcurementRukuMateriel  prm : list){
			
			 a = materialComeKuService.savemck(prm,suprname,number,nowtime);
			
		}
		//修改资材入库单状态
		if(a > 0){
			materialComeKuService.updateProcurementRuku(ids);
		}
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", "1");
		jsonObject.put("data", a);
		ResponseUtil.write(response, jsonObject);
	}
	
	
}
